/**
 * Notes: 员工控制器 - 宠物门店预约小程序
 * Date: 2025-01-27
 */

const BaseController = require('./base_controller.js');
const EmployeeService = require('../service/employee_service.js');
const timeUtil = require('../../framework/utils/time_util.js');
const UserModel = require('../model/user_model.js');
const PassportService = require('../service/passport_service.js');
const config = require('../../config/config.js');

class EmployeeController extends BaseController {

	/**
	 * 获取员工列表
	 */
	async getEmployeeList() {
		// 数据校验
		let rules = {
			storeId: 'string|name=门店ID',
			grade: 'string|name=员工等级'
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new EmployeeService();
		let result = await service.getEmployeeList(input.storeId, input.grade);

		return result;
	}

	/**
	 * 获取员工详情
	 */
	async getEmployeeDetail() {
		// 数据校验
		let rules = {
			employeeId: 'must|string|name=员工ID'
		};

		// 取得数据
		let input = this.validateData(rules);

		let service = new EmployeeService();
		let result = await service.getEmployeeDetail(input.employeeId);

		return result;
	}

	/**
	 * 添加员工
	 */
	async addEmployee() {
		this.ensureAuthenticatedForWrite();
		// 数据校验
		let rules = {
			storeId: 'must|string|min:1|max:50|name=门店ID',
			name: 'must|string|min:1|max:20|name=员工姓名',
			gender: 'string|in:男,女|name=性别',
			phone: 'string|max:20|name=电话',
			role: 'string|max:50|name=角色',
			avatar: 'string|max:200|name=头像',
			desc: 'string|max:200|name=简介'
		};

		let input = this.validateData(rules);

		// TEST_MODE 下直接返回模拟数据，避免访问云数据库
		if (config.TEST_MODE) {
			return {
				_id: 'emp_test_' + Date.now(),
				storeId: input.storeId,
				userId: this._userId,
				name: input.name,
				gender: input.gender || '男',
				phone: input.phone || '',
				role: input.role || '员工',
				avatar: input.avatar || '',
				desc: input.desc || '',
				createTime: this._timestamp,
				updateTime: this._timestamp
			};
		}

		// 用户存在性预检与自动建档兜底（非测试模式）
		let user = await UserModel.getOne({ _id: this._userId });
		if (!user) {
			const passport = new PassportService();
			await passport.registerUser(this._userId, {});
		}

		let service = new EmployeeService();
		return await service.addEmployee(this._userId, input);
	}

	/**
	 * 更新员工信息
	 */
	async updateEmployee() {
		this.ensureAuthenticatedForWrite();
		// 数据校验
		let rules = {
			employeeId: 'must|string|min:1|max:50|name=员工ID',
			name: 'string|min:1|max:20|name=员工姓名',
			gender: 'string|in:男,女|name=性别',
			phone: 'string|max:20|name=电话',
			role: 'string|max:50|name=角色',
			avatar: 'string|max:200|name=头像',
			desc: 'string|max:200|name=简介'
		};

		let input = this.validateData(rules);

		// TEST_MODE 下直接返回模拟数据，避免访问云数据库
		if (config.TEST_MODE) {
			return {
				_id: input.employeeId,
				userId: this._userId,
				storeId: input.storeId,
				name: input.name,
				gender: input.gender,
				phone: input.phone,
				role: input.role,
				avatar: input.avatar,
				desc: input.desc,
				updateTime: this._timestamp
			};
		}

		// 用户存在性预检与自动建档兜底（非测试模式）
		let user = await UserModel.getOne({ _id: this._userId });
		if (!user) {
			const passport = new PassportService();
			await passport.registerUser(this._userId, {});
		}

		let service = new EmployeeService();
		return await service.updateEmployee(input.employeeId, this._userId, input);
	}

	/**
	 * 删除员工
	 */
	async deleteEmployee() {
		this.ensureAuthenticatedForWrite();
		// 数据校验
		let rules = {
			employeeId: 'must|string|min:1|max:50|name=员工ID',
		};
		let input = this.validateData(rules);

		// TEST_MODE 下直接返回模拟数据，避免访问云数据库
		if (config.TEST_MODE) {
			return true;
		}

		// 用户存在性预检与自动建档兜底（非测试模式）
		let user = await UserModel.getOne({ _id: this._userId });
		if (!user) {
			const passport = new PassportService();
			await passport.registerUser(this._userId, {});
		}

		let service = new EmployeeService();
		return await service.deleteEmployee(input.employeeId, this._userId);
	}

}

module.exports = EmployeeController;